﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonFramework.Utility;
using Back.Xinkui.WebModel;
using Back.Xinkui.DataAccess.Advert;
using Back.Xinkui.Entity.Advert;
using Back.Xinkui.Common.Enum;
using Xinkui.Common.FileAction;
using Xinkui.Common.Enum;
using Xinkui.Common;

public partial class advert_advertDetail : AdvertAspxBase
{
    private int id
    {
        get { return WebHelper.GetQueryStringValue<int>("ID", 0); }
    }
    private int spaceId
    {
        get { return WebHelper.GetQueryStringValue<int>("SpaceID", 0); }
    }
    private int boardId = 1;

    protected string action
    {
        get { return WebHelper.GetQueryStringValue<string>("Action", string.Empty); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (action == "edit")
        {
            ResponseScript("$('.SpaceIDBlock').hide();");
            advertise = AdvertDataAccess.GetAdvertByID(id);
            if (advertise.Status == 3)
            {
                advertise.Status = 2;
            }
            space = AdvertDataAccess.GetAdvertSpaceByID(advertise.SpaceID);
        }
        if (string.IsNullOrEmpty(WebHelper.GetFormValue<string>("AdvertNo", "")))
        {
            InitData();
        }
        else
        {
            Action();
        }
    }

    protected AdvertEntity advertise = new AdvertEntity();
    protected string uploadTip = "";
    private void InitData()
    {
        if (action == "add")
        {
            advertise.BoardID = boardId;
            advertise.SpaceID = spaceId;
            if (spaceId > 0)
            {
                space = AdvertDataAccess.GetAdvertSpaceByID(spaceId);
            }
            advertise.AdvertType = space.SourceType;
            advertise.BeginTime = DateTime.Now;
            advertise.EndTime = DateTime.Now.AddYears(1);
        }
    }
    protected AdvertSpaceEntity space = new AdvertSpaceEntity();
    private void Action()
    {
        if (action == "edit")
        {
            AdvertEntity temp = AdvertDataAccess.GetAdvertByID(id);
            if (temp != null)
            {
                advertise = temp;
            }
        }
        if (action == "add")
        {
            advertise.BoardID = boardId;//页面用，不会用到数据库
        }
        advertise.ID = id;
        advertise.SpaceID = WebHelper.GetFormValue<int>("SpaceID", (int)ESpecialKey.IntNoSearch);
        space = AdvertDataAccess.GetAdvertSpaceByID(advertise.SpaceID);
        advertise.AdvertNo = WebHelper.GetFormValue<string>("AdvertNo", "");
        advertise.AdvertName = WebHelper.GetFormValue<string>("AdvertName", "");
        advertise.AdvertType = WebHelper.GetFormValue<int>("AdvertType", (int)ESpecialKey.IntNoSearch);
        advertise.Text = WebHelper.GetFormValue<string>("Text", "");
        advertise.Alt = WebHelper.GetFormValue<string>("Alt", "");
        advertise.Description = WebHelper.GetFormValue<string>("Description", "");
        advertise.KeyWords = WebHelper.GetFormValue<string>("KeyWords", "");
        advertise.Url = WebHelper.GetFormValue<string>("Url", "");
        advertise.BeginTime = WebHelper.GetFormValue<DateTime>("BeginTime", DateTime.Now);
        advertise.EndTime = WebHelper.GetFormValue<DateTime>("EndTime", DateTime.Now);
        advertise.CompanyCrmID = WebHelper.GetFormValue<int>("CompanyCrmID", 0);
        advertise.Status = WebHelper.GetFormValue<int>("Status", 0);

        if (FileUpload1.PostedFile.FileName.Length > 0)
        {
            //string WpicOld = advertise.AdvertPath;
            FileEntity fileentity = Xinkui.Common.FileUpload.Upload(FileUpload1.PostedFile, ESourceType.AD, 0, 0);
            string filePath = fileentity.SavePath;
            //判断图片长宽是否合适            
            if (space != null && space.Width > 0 && space.Height > 0 && (filePath.Contains("gif") || filePath.Contains("jpg") || filePath.Contains("jpeg") || filePath.Contains("png") || filePath.Contains("bmp")))
            {
                System.Drawing.Image originalImage = System.Drawing.Image.FromFile(fileentity.ServerPath);
                string alterStr = "";
                if (originalImage.Width > space.Width)
                {
                    alterStr = "允许上传图片最大宽度为：" + space.Width + "。 上传图片宽度 " + originalImage.Width + " ！ ";
                }
                if (originalImage.Height > space.Height)
                {
                    alterStr += "允许上传图片最大长度为：" + space.Height + "。 上传图片长度 " + originalImage.Height + " ！ ";
                }
                if (!string.IsNullOrEmpty(alterStr))
                {
                    Show(alterStr);
                    return;
                }
            }

            if (!string.IsNullOrEmpty(filePath))
            {
                advertise.AdvertPath = filePath;
            }
        }
        if (FileUpload2.PostedFile.FileName.Length > 0)
        {
            FileEntity fileentity = Xinkui.Common.FileUpload.Upload(FileUpload2.PostedFile, ESourceType.AD, 0, 0);
            string filePath = fileentity.SavePath;
            //判断图片长宽是否合适            
            if (space != null && space.BWidth > 0 && space.BHeight > 0 && (filePath.Contains("gif") || filePath.Contains("jpg") || filePath.Contains("jpeg") || filePath.Contains("png") || filePath.Contains("bmp")))
            {
                System.Drawing.Image originalImage = System.Drawing.Image.FromFile(fileentity.ServerPath);
                string alterStr = "";
                if (originalImage.Width > space.BWidth)
                {
                    alterStr = "允许上传图片最大宽度为：" + space.BWidth + "。 上传图片宽度 " + originalImage.Width + " ！ ";
                }
                if (originalImage.Height > space.BHeight)
                {
                    alterStr += "允许上传图片最大长度为：" + space.BHeight + "。 上传图片长度 " + originalImage.Height + " ！ ";
                }
                if (!string.IsNullOrEmpty(alterStr))
                {
                    Show(alterStr);
                    return;
                }
            }

            if (!string.IsNullOrEmpty(filePath))
            {
                advertise.BAdvertPath = filePath;
            }
        }

        if (string.IsNullOrEmpty(advertise.AdvertNo) || string.IsNullOrEmpty(advertise.AdvertName) || string.IsNullOrEmpty(advertise.Text)
            || string.IsNullOrEmpty(advertise.Alt) || string.IsNullOrEmpty(advertise.Description)
            || advertise.AdvertType == (int)ESpecialKey.IntNoSearch || string.IsNullOrEmpty(advertise.BeginTime.ToString()) || string.IsNullOrEmpty(advertise.EndTime.ToString())
                     )
        {
            Show("请填写完整！");
            return;
        }

        if (action == "add")
        {
            AdvertDataAccess.CreateAdvert(advertise);
            Show("添加成功！");
        }
        if (action == "edit")
        {
            AdvertDataAccess.UpdateAdvert(advertise);
            Show("编辑成功！");
        }
    }

}